package name.pilgr.soaboard.view;

import java.util.Vector;

import com.vaadin.data.Container;
import com.vaadin.data.Container.Indexed;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Table;

import name.pilgr.soaboard.model.DataService;

@SuppressWarnings("serial")
public class ObservantPanelTable extends ObservantPanel {

	Table table;
	IndexedContainer container;
	
	public ObservantPanelTable(DataService dataService) {
		super(dataService);
		
		table = new Table();
		table.setStyleName("iso3166");
		table.setWidth("100%");
		table.setHeight("100%");
        // selectable
        table.setSelectable(true);
        table.setImmediate(true); // react at once when something is selected
        // turn on column reordering and collapsing
        table.setColumnReorderingAllowed(true);
        table.setColumnCollapsingAllowed(true);
		
        container = new IndexedContainer();
        table.setContainerDataSource(container);
		panel.addComponent(table);
	}
	
	void visualize(Vector<Vector<String>> currentData) {
		// by row in all vector
		container.removeAllItems();
				
		IndexedContainer tmpContainer = new IndexedContainer();
		int numCol = currentData.get(0).size();
		int numRow = currentData.size() - 1;//First row - name of columns
		
        //Init column names
		Vector<String> columnNames = currentData.get(0);
		for (int iCol=0; iCol<numCol; iCol++) {
			tmpContainer.addContainerProperty(columnNames.get(iCol), String.class, "");
        }
		
		// Init rows
		// Skip row #0 in currentData. This row contain meta data - column names
		for (int iRow = 1; iRow < numRow; iRow++) {
			Vector<String> currRow = currentData.get(iRow);
			// Add row to container
			Object itemId = tmpContainer.addItem();
			for (int iCol = 0; iCol < numCol; iCol++) {
				//Filling data in each cell of row
				tmpContainer
						.getContainerProperty(itemId, columnNames.get(iCol))
						.setValue(currRow.get(iCol));
			}
		}
		
		container = tmpContainer;
		table.setContainerDataSource(container);
		
		
		/*Object itemId = ic.addItem();
		ic.getContainerProperty(itemId, "ID").setValue("Value");*/
		
		//container = tmpContainer;
		/*ic.addContainerProperty("PropID", String.class, "");
		Object itemId = ic.addItem();
		ic.getContainerProperty(itemId, "PropID").setValue("Value");
		
		table.setContainerDataSource(ic);*/
		
		/*for (int iRow = 1; iRow < currentData.size(); iRow++){
			Vector<String> row;
			row = currentData.get(iRow);
		
			// by elements in row
			for (int iElem = 0; iElem < row.size(); iElem ++){
				curString +="|" + row.get(iElem).toString();
			}
			curString += "<br>";
		}
		panelText.setValue(curString);*/
	}
	

}
